/**
 * @(#) FileUploadProxy.as
 *
 * @date		2009. 08. 04.
 * @version		1.0
 * @author		Park, JuneHo
 * @description	 공지사항 쓰기 - Proxy
 * 				공지사항 조회하는 Model Logic
 */
package com.skt.common_hr.allapp.model
{
	import com.skt.common_hr.common.puremvc.CommonProxy;
	import com.skt.common_hr.allapp.model.vo.NoticeFileUpLoadVO;

	import flash.events.DataEvent;
	import flash.events.Event;
	import flash.events.IOErrorEvent;
	import flash.events.ProgressEvent;
	import flash.net.FileReference;
	import flash.net.URLRequest;

	public class AttachFileProxy extends CommonProxy
	{
		public static const NAME:String = "AttachFileProxy";

		public static const SELECTED_FILE:String = "selectedFile";
		public static const PROGRESS_UPLOAD:String = "progressUpload";
		public static const COMPLETE_UPLOAD:String = "completeUpload";
		public static const FAILED_UPLOAD:String = "failedUpload";

		private var fileRef:FileReference;

		public var fileVo:NoticeFileUpLoadVO;
		public var bytesLoaded:uint = 0;
		public var bytesTotal:uint = 0;

		public function AttachFileProxy(data:Object=null)
		{
			super(NAME, data);
		}

		public function browse():void
		{
			fileVo = new NoticeFileUpLoadVO();

			fileRef = new FileReference();
            fileRef.addEventListener(Event.SELECT, selectedFile);
            fileRef.addEventListener(ProgressEvent.PROGRESS, onProgress);
            // 저장된 Director를 얻기 위해 Event.COMPLETE 대신 DataEvent를 사용한다.
            //fileRef.addEventListener(Event.COMPLETE, onCompleteUpload);
            fileRef.addEventListener(DataEvent.UPLOAD_COMPLETE_DATA, onCompleteData);
            fileRef.addEventListener(IOErrorEvent.IO_ERROR, onIOError);
            fileRef.browse();
		}

		public function deleteFile():void
		{

		}

		private function selectedFile(e:Event):void
        {
        	fileVo.I_FILE_NAME = (fileRef.name==null) ? "":fileRef.name; //replaceAll(fileRef.name," ","_");
			fileVo.I_FILE_EXT  = (fileRef.type==null) ? "":fileRef.type.substr(1);
			fileVo.I_FILE_SIZE = fileRef.size;

			facade.sendNotification(SELECTED_FILE, this);
			//uploadFile();
        }

		public function uploadFile():void
		{
			var urlRequest:URLRequest = new URLRequest("../FileUpload");
			//var urlRequest:URLRequest = new URLRequest("../upload");
			fileRef.upload(urlRequest);
		}

		private function onProgress(event:ProgressEvent):void
		{
			bytesLoaded = event.bytesLoaded;
			bytesTotal  = event.bytesTotal;
			facade.sendNotification(PROGRESS_UPLOAD, this);
		}

		private function onCompleteUpload(event:Event):void
		{
			bytesLoaded = 0;
			bytesTotal  = 0;
			facade.sendNotification(COMPLETE_UPLOAD, this);
		}

		private function onCompleteData(event:DataEvent):void
		{
			fileVo.I_FILE_URL = event.data;
			facade.sendNotification(COMPLETE_UPLOAD, this);
		}

		private function onIOError(event:IOErrorEvent):void
		{
			trace("IOError: " + event);
			facade.sendNotification(FAILED_UPLOAD, this);
		}

 		private function replaceAll(str:String, strPattern:String, strObject:String ):String
 		{
	    	if (str == "" || str == null) return "";
				while(str.indexOf(strPattern) >= 0)
				{
					str = str.replace(strPattern,strObject);
				}
				return str;
		}
	}
}